package com.dld.coupon.db;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.dld.coupon.util.MapUtil;
import com.dld.coupon.util.StringUtils;

public class Category {
	public int id;
	public String name;
	public Integer pid;
	public Integer code;

	public static Category get(SQLiteDatabase paramSQLiteDatabase, int paramInt) {
		Category localCategory;
		if (paramInt != 100) {
			Cursor localCursor = paramSQLiteDatabase.rawQuery(
					"SELECT * FROM category WHERE code = " + paramInt, null);
			localCursor.moveToFirst();
			localCategory = toCategory(localCursor);
			localCursor.close();
		} else {
			localCategory = new Category();
			localCategory.pid = Integer.valueOf(0);
			localCategory.id = 1;
			localCategory.code = 100;
			localCategory.name = "全部分类";
		}
		return localCategory;
	}

	public static List<Category> list(SQLiteDatabase paramSQLiteDatabase) {
		ArrayList<Category> localArrayList = new ArrayList<Category>();
		localArrayList.add(get(paramSQLiteDatabase, 100));
		Cursor localCursor = paramSQLiteDatabase.rawQuery(
				"SELECT * FROM category WHERE pid = 1", null);
		while (localCursor.moveToNext()) {
			localArrayList.add(toCategory(localCursor));
		}
		localCursor.close();
		return localArrayList;
	}

	public static List<Category> list(SQLiteDatabase paramSQLiteDatabase,
			Category paramCategory) {
		ArrayList<Category> localArrayList = new ArrayList<Category>();
		localArrayList.add(paramCategory);
		Cursor cursor = paramSQLiteDatabase.rawQuery(
				"SELECT * FROM category WHERE pid = " + paramCategory.id, null);
		while (cursor.moveToNext()) {
			Category cg = toCategory(cursor);
			localArrayList.add(cg);
		}
		cursor.close();
		if (localArrayList.size() >= 2) {
			Category localCategory = (Category) localArrayList.get(1);
			String city_str = MapUtil.getCity();
			if ((StringUtils.equals(localCategory.name, "北京菜"))
					&& (!StringUtils.equals((String) city_str, "北京")))
				localArrayList.remove(1);
		}
		return (List<Category>) localArrayList;
	}

	private static Category toCategory(Cursor paramCursor) {
		Category localCategory = new Category();
		try {
			localCategory.id = paramCursor.getInt(paramCursor
					.getColumnIndex("id"));
			localCategory.pid = Integer.valueOf(paramCursor.getInt(paramCursor
					.getColumnIndex("pid")));
			localCategory.name = paramCursor.getString(paramCursor
					.getColumnIndex("name"));
			localCategory.code = paramCursor.getInt(paramCursor
					.getColumnIndex("code"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return localCategory;
	}

	public boolean equals(Category paramObject) {
		boolean i = paramObject.id == this.id;
		return i;
	}
}
